'use strict';
module.exports = {
	/**
	 * XXXnameXXX
	 * @url admin/pages/pub/queryStatus 前端调用的url参数地址
	 * data 请求参数
	 * @param {String} params1  参数1
	 */
	main: async (event) => {
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
			db,
			_
		} = util;
		let {
			uid,
			appid,
			appsecret,
			album_id //剧集id
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		if (!appsecret) {
			let odata = await db.collection("a_appid").where({
				appid: appid,
			}).get()
			res.data = odata
			appsecret = odata.data[0].appsecret
		}

		// 获取token
		let urlA = "https://open.douyin.com/oauth/client_token/"
		const accessToken = await uniCloud.httpclient.request(urlA, {
			method: 'POST',
			headers: {
				"Content-Type": "application/json",
			},
			data: {
				client_key: appid, // id
				client_secret: appsecret, // 密钥
				grant_type: "client_credential",
			},
			dataType: 'json', // 指定返回值为json格式，自动进行parseK
		})
		let access_token = accessToken.data.data.access_token

		let urlB = "https://open.douyin.com/api/playlet/v2/album/fetch/"
		const resData = await uniCloud.httpclient.request(urlB, {
			method: 'POST',
			headers: {
				"Content-Type": "application/json",
				"access-token": access_token
			},
			data: {
				"ma_app_id": appid,
				"query_type": 2,
				"single_query": {
					"limit": 20,
					"offset": 0,
					"album_id": album_id
				}
			},
			dataType: 'json', // 指定返回值为json格式，自动进行parseK
		})
		console.log("审核查询11111111111111111111111", resData.data.data.single_data.album_info_list);
		let album_info = resData.data.data.single_data.album_info_list.pop()
		let statusInfo = album_info.album_audit_info
		res.tvName = album_info.title
		if (statusInfo.status == 1) {
			res.code = 1
			res.msg = "未提交审核"
		}
		if (statusInfo.status == 2) {
			res.code = 1
			res.msg = "版本" + statusInfo.version + " 已提交，未审出"
			await vk.baseDao.update({
				dbName: "dy_tv", // 表名
				whereJson: { // 条件
					album_id: album_id,
				},
				dataJson: { // 需要修改的数据
					audit_status: 2
				}
			});
		} else if (statusInfo.status == 3 && statusInfo.audit_status == 1) {
			res.code = 1
			res.msg = "版本" + statusInfo.version + " 审核未通过，不可播放"
			if (statusInfo.scope_list) {
				res.scope_list = statusInfo.scope_list
			}
			if (statusInfo.audit_msg) {
				res.audit_msg = statusInfo.audit_msg
			}
			//更新数据库
			await vk.baseDao.update({
				dbName: "dy_tv", // 表名
				whereJson: { // 条件
					album_id: album_id,
				},
				dataJson: { // 需要修改的数据
					audit_status: 4,
					audit_msg: res.audit_msg,
				}
			});
		} else if (statusInfo.status == 3 && statusInfo.audit_status == 2) {
			res.code = 1
			res.msg = "版本" + statusInfo.version + " 审核通过，可播放"
			if (statusInfo.scope_list) {
				res.scope_list = statusInfo.scope_list
			}
			if (statusInfo.audit_msg) {
				res.audit_msg = statusInfo.audit_msg
			}
			await vk.baseDao.update({
				dbName: "dy_tv", // 表名
				whereJson: { // 条件
					album_id: album_id,
				},
				dataJson: { // 需要修改的数据
					audit_status: 3,
					audit_msg: res.audit_msg,
					scope_list: res.scope_list
				}
			});
		}


		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}